home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware in MacFormat / brailler0.5b / brlr ƒ / Shell ƒ / buttons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-20  |  5.3 KB  |  194 lines  |  [TEXT/MMCC]

  1. #include "buttons.h"
  2.  
  3. static    RGBColor        gBlack4Bit={32768, 32768, 32768};
  4. static    RGBColor        gWhite4Bit={60948, 60948, 60948};
  5. static    RGBColor        gBackground4Bit={49152,49152,49152};
  6. static    RGBColor        gBlack8Bit={26214, 26214, 39322};
  7. static    RGBColor        gWhite8Bit={60948, 60948, 60948};
  8. static    RGBColor        gBackground8Bit={52429, 52429, 52429};
  9.  
  10. #define TRIANGLE_SIZE    6
  11.  
  12. void Draw3DButton(Rect *buttonRect, unsigned char *theTitle,
  13.     Handle iconHandle, short buttonDepth, Boolean isDown, Boolean drawTriangle)
  14. {
  15.     FontInfo        theFontInfo;
  16.     Rect            tempRect;
  17.     RGBColor        oldForeColor, oldBackColor;
  18.     PixPatHandle    backgroundppat;
  19.     short            theLineHeight;
  20.     Rect            iconRect;
  21.     RGBColor        myWhite={65535, 65535, 65535};
  22.     
  23.     TextFont(geneva);
  24.     TextSize(9);
  25.     TextFace(0);
  26.     GetFontInfo(&theFontInfo);
  27.     theLineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading;
  28.     if (iconHandle!=0L)
  29.     {
  30.         iconRect.left=(buttonRect->right-buttonRect->left)/2+buttonRect->left-16+
  31.             (isDown ? 1 : 0);
  32.         iconRect.right=iconRect.left+32;
  33.         iconRect.top=(buttonRect->bottom-buttonRect->top)/2+buttonRect->top-16+
  34.             (isDown ? 1 : 0)-((theTitle!=0L) ? 5 : 0);
  35.         iconRect.bottom=iconRect.top+32;
  36.     }
  37.     
  38.     if (buttonDepth>2)
  39.     {
  40.         GetForeColor(&oldForeColor);
  41.         GetBackColor(&oldBackColor);
  42.         
  43.         backgroundppat=NewPixPat();
  44.         MakeRGBPat(backgroundppat, (buttonDepth==4) ? &gBackground4Bit :
  45.             &gBackground8Bit);
  46.         SetRect(&tempRect, buttonRect->left+2, buttonRect->top+2,
  47.             buttonRect->right-2, buttonRect->bottom-2);
  48.         
  49.         FillCRect(&tempRect, backgroundppat);
  50.  
  51.         RGBBackColor(&myWhite);
  52.         RGBForeColor(isDown ? ((buttonDepth==4) ? &gBlack4Bit : &gBlack8Bit) :
  53.             ((buttonDepth==4) ? &gWhite4Bit : &gWhite8Bit));
  54.         MoveTo(buttonRect->left, buttonRect->top);
  55.         LineTo(buttonRect->right-2, buttonRect->top);
  56.         MoveTo(buttonRect->left, buttonRect->top+1);
  57.         LineTo(buttonRect->right-3, buttonRect->top+1);
  58.         MoveTo(buttonRect->left, buttonRect->top+2);
  59.         LineTo(buttonRect->left, buttonRect->bottom-2);
  60.         MoveTo(buttonRect->left+1, buttonRect->top+2);
  61.         LineTo(buttonRect->left+1, buttonRect->bottom-3);
  62.         
  63.         RGBForeColor(isDown ? ((buttonDepth==4) ? &gWhite4Bit : &gWhite8Bit) :
  64.             ((buttonDepth==4) ? &gBlack4Bit : &gBlack8Bit));
  65.         MoveTo(buttonRect->right-1, buttonRect->top);
  66.         LineTo(buttonRect->right-1, buttonRect->bottom-1);
  67.         MoveTo(buttonRect->right-2, buttonRect->top+1);
  68.         LineTo(buttonRect->right-2, buttonRect->bottom-1);
  69.         MoveTo(buttonRect->left+2, buttonRect->bottom-2);
  70.         LineTo(buttonRect->right-1, buttonRect->bottom-2);
  71.         MoveTo(buttonRect->left+1, buttonRect->bottom-1);
  72.         LineTo(buttonRect->right-1, buttonRect->bottom-1);
  73.     }
  74.     else
  75.     {
  76.         EraseRect(buttonRect);
  77.         FrameRect(buttonRect);
  78.         if (!isDown)
  79.         {
  80.             MoveTo(buttonRect->left+2, buttonRect->bottom-2);
  81.             LineTo(buttonRect->right-2, buttonRect->bottom-2);
  82.             LineTo(buttonRect->right-2, buttonRect->top+2);
  83.         }
  84.         else
  85.         {
  86.             MoveTo(buttonRect->left+1, buttonRect->bottom-3);
  87.             LineTo(buttonRect->left+1, buttonRect->top+1);
  88.             LineTo(buttonRect->right-3, buttonRect->top+1);
  89.         }
  90.     }
  91.     
  92.     if (theTitle!=0L)
  93.     {
  94.         ForeColor(blackColor);
  95.         MoveTo(buttonRect->left+(buttonRect->right-buttonRect->left)/2-
  96.             (StringWidth(theTitle)/2)+(isDown ? 1 : 0), ((iconHandle!=0L) ?
  97.             buttonRect->bottom-6 : buttonRect->top+
  98.             (buttonRect->bottom-buttonRect->top)/2+theLineHeight/2-2)+
  99.             (isDown ? 1 : 0));
  100.         DrawString(theTitle);
  101.     }
  102.     
  103.     if (iconHandle!=0L)
  104.     {
  105.         if (buttonDepth>2)
  106.             PlotCIcon(&iconRect, (CIconHandle)iconHandle);
  107.         else
  108.         {
  109.             BitMap    iconMap;
  110.             GrafPtr curPort;
  111.             
  112.             GetPort(&curPort);
  113.             HLock( iconHandle);    /* lock data in place */
  114.             
  115.             iconMap.baseAddr = *iconHandle;    /* dereference the handle */
  116.             iconMap.rowBytes = 4;            /* setup other fields */
  117.             SetRect( &iconMap.bounds, 0,0,32,32);
  118.             
  119.             CopyBits( &iconMap, &(curPort->portBits),
  120.                          &iconMap.bounds, &iconRect, srcOr, 0L );
  121.             
  122.             HUnlock(iconHandle);                /* all done; let it float */
  123.         }
  124.     }
  125.     
  126.     if (drawTriangle)
  127.     {
  128.         RgnHandle        triangleRgn;
  129.         
  130.         triangleRgn=NewRgn();
  131.         OpenRgn();
  132.             MoveTo(buttonRect->right-6+(isDown ? 1 : 0), buttonRect->top+5+(isDown ? 1 : 0));
  133.             Line(-TRIANGLE_SIZE, TRIANGLE_SIZE);
  134.             Line(-TRIANGLE_SIZE, -TRIANGLE_SIZE);
  135.             Line(TRIANGLE_SIZE*2, 0);
  136.         CloseRgn(triangleRgn);
  137.         FillRgn(triangleRgn, &qd.black);
  138.         DisposeRgn(triangleRgn);
  139.     }
  140.     
  141.     if (buttonDepth>2)
  142.     {
  143.         DisposPixPat(backgroundppat);
  144.         RGBForeColor(&oldForeColor);
  145.         RGBBackColor(&oldBackColor);    
  146.     }
  147. }
  148.  
  149. Boolean Track3DButton(Rect *buttonRect, unsigned char *theTitle,
  150.     Handle iconHandle, short buttonDepth, Boolean drawTriangle)
  151. {
  152.     Point            mouseLoc;
  153.     Boolean            buttonIsDown;
  154.     
  155.     buttonIsDown=FALSE;
  156.     while (StillDown())
  157.     {
  158.         GetMouse(&mouseLoc);
  159.         if (PtInRect(mouseLoc, buttonRect))
  160.         {
  161.             if (!buttonIsDown)
  162.             {
  163.                 buttonIsDown=TRUE;
  164.                 Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth,
  165.                     TRUE, drawTriangle);
  166.             }
  167.         }
  168.         else
  169.         {
  170.             if (buttonIsDown)
  171.             {
  172.                 buttonIsDown=FALSE;
  173.                 Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth,
  174.                     FALSE, drawTriangle);
  175.             }
  176.         }
  177.     }
  178.     
  179.     if (buttonIsDown)
  180.         Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, FALSE, drawTriangle);
  181.     
  182.     return buttonIsDown;
  183. }
  184.  
  185. void Hit3DButton(Rect *buttonRect, unsigned char *theTitle,
  186.     Handle iconHandle, short buttonDepth, Boolean drawTriangle)
  187. {
  188.     long            dummy;
  189.     
  190.     Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, TRUE, drawTriangle);
  191.     Delay(8, &dummy);
  192.     Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, FALSE, drawTriangle);
  193. }
  194.